package com.hupu.jrs.ui.thread.recommend

import com.hupu.jrs.Constants
import com.hupu.jrs.data.ThreadRepository
import com.hupu.jrs.injector.PerActivity
import com.hupu.jrs.util.ToastUtil
import java.util.ArrayList
import javax.inject.Inject
import rx.Subscription
import rx.functions.Action1
import rx.subjects.PublishSubject

/**
 * Created by sll on 2016/3/9.
 */
@PerActivity
class ThreadRecommendPresenter @Inject
constructor(private val mThreadRepository: ThreadRepository) : RecommendThreadListContract.Presenter {

    private val mThreadSubject: PublishSubject<List<Thread>>
    private var isFirst = true
    private var threads: List<Thread> = ArrayList()

    private var mSubscription: Subscription? = null
    private var mRecommendView: RecommendThreadListContract.View? = null
    private var lastTid = ""
    private var lastTamp = ""
    private var hasNextPage = true

    init {
        mThreadSubject = PublishSubject.create()
    }

    override fun onThreadReceive() {
        mRecommendView!!.showLoading()
        mThreadRepository.getThreadListObservable(Constants.TYPE_RECOMMEND, mThreadSubject)
                .subscribe(Action1<List<Thread>> { threads ->
                    this@ThreadRecommendPresenter.threads = threads
                    if (threads.isEmpty()) {
                        if (!isFirst) {
                            mRecommendView!!.onError("数据加载失败")
                        }
                        isFirst = false
                    } else {
                        mRecommendView!!.showContent()
                        if (!threads.isEmpty()) {
                            lastTid = threads[threads.size - 1].getTid()
                        }
                        mRecommendView!!.renderThreads(threads)

                    }
                })
        loadRecommendList()
    }

    private fun loadRecommendList() {
        mSubscription = mThreadRepository.getRecommendThreadList(lastTid, lastTamp, mThreadSubject)
                .subscribe(Action1<Any> { threadListData ->
                    if (threadListData != null && threadListData.result != null) {
                        val data = threadListData.result
                        lastTamp = data.stamp
                        hasNextPage = data.nextPage
                    }
                    mRecommendView!!.onRefreshCompleted()
                    mRecommendView!!.onLoadCompleted(hasNextPage)
                }, Action1<Throwable> {
                    if (threads.isEmpty()) {
                        mRecommendView!!.onError("数据加载失败，请重试")
                    } else {
                        mRecommendView!!.onRefreshCompleted()
                        mRecommendView!!.onLoadCompleted(hasNextPage)
                        ToastUtil.showToast("数据加载失败，请重试")
                    }
                })
    }

    override fun onRefresh() {
        lastTamp = ""
        lastTid = ""
        loadRecommendList()
    }

    override fun onReload() {
        onThreadReceive()
    }

    override fun onLoadMore() {
        if (!hasNextPage) {
            ToastUtil.showToast("没有更多了~")
            mRecommendView!!.onLoadCompleted(false)
            return
        }
        loadRecommendList()
    }

    fun attachView(view: RecommendThreadListContract.View) {
        mRecommendView = view
    }

    fun detachView() {
        if (mSubscription != null && !mSubscription!!.isUnsubscribed) {
            mSubscription!!.unsubscribe()
        }
        mRecommendView = null
    }
}
